package com.hbzd.protection.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.sword.wechat4j.user.TemData;
import org.sword.wechat4j.user.UserManager;

import com.hbzd.protection.entity.Employee;
import com.hbzd.protection.service.EmployeeServiceImpl;


@Controller
@RequestMapping("/login")
public class EmployeeController {
	
	@Resource
	private EmployeeServiceImpl employeeService; 
	
	/**
	 * 用户点击账号绑定时，首先判断该用户是否已绑定。如若绑定，则跳转到 提示界面，
	 * 获得openid，并跳转到登陆界面
	 * @param code
	 * @param request
	 * @param response
	 * @param modalMap
	 * @return
	 */
	@RequestMapping("/auth")
	public String authenticate(String code,String state,HttpServletRequest request, HttpServletResponse response, ModelMap modalMap) {
		UserManager userManager = new UserManager();
		TemData temData = userManager.getOpenId(code);
		if(employeeService.authenticate(temData.getOpenid())){
			//绑定状态
			if("login".equals(state)){
				return "redirect:/handle/deal?code=login";
			}else if("modify".equals(state)){
				//修改密码状态
				modalMap.put("openid", temData.getOpenid());
				return "/weixin/modify";
			}else if("unauth".equals(state)){
				//解除绑定
				modalMap.put("openid", temData.getOpenid());
				return "/weixin/unauth";
			}
		}
		modalMap.put("openid", temData.getOpenid());
		return "/weixin/login";
	}
	/**
	 * 将用户openid和phone进行绑定
	 * @param phone
	 * @param password
	 * @param openid
	 * @param request
	 * @param response
	 * @param modalMap
	 * @return
	 */
	@RequestMapping("/access")
	public void access(Employee employee ,HttpServletRequest request, HttpServletResponse response, ModelMap modalMap) {
		String result = employeeService.handleData(employee);
		try {
			response.getWriter().print(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 修改密码
	 * @param employee
	 * @param password1
	 * @param request
	 * @param response
	 * @param modalMap
	 */
	@RequestMapping("/modify")
	public void modify(Employee employee ,String password1, HttpServletRequest request, HttpServletResponse response, ModelMap modalMap) {
		String result = employeeService.modifyPassword(employee,password1);
		try {
			response.getWriter().print(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 解除绑定
	 * @param employee
	 * @param request
	 * @param response
	 * @param modalMap
	 */
	@RequestMapping("/unauth")
	public void unAuth(Employee employee ,HttpServletRequest request, HttpServletResponse response, ModelMap modalMap) {
		String result = employeeService.update(employee);
		try {
			response.getWriter().print(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
